package com.xbongbong.paas.controller;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.data.service.CabinetFileService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.cabinetfile.pojo.dto.CabinetFileDeleteDTO;
import com.xbongbong.pro.cabinetfile.pojo.dto.CabinetFileDownLoadLogDTO;
import com.xbongbong.pro.cabinetfile.pojo.dto.CabinetFileListGetDTO;
import com.xbongbong.pro.cabinetfile.pojo.dto.CabinetFileSaveDTO;
import com.xbongbong.pro.cabinetfile.pojo.vo.CabinetFileDeleteVO;
import com.xbongbong.pro.cabinetfile.pojo.vo.CabinetFileDownLoadLogVO;
import com.xbongbong.pro.cabinetfile.pojo.vo.CabinetFileListGetVO;
import com.xbongbong.pro.cabinetfile.pojo.vo.CabinetFileSaveVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/6/25 19:40
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/cabinetFile")
public class CabinetFileController {
    @Resource
    private CabinetFileService cabinetFileService;

    /**
     * Description: 文件柜列表
     * @param cabinetFileListGetDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/6/25 19:43
     * @since v1.0
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CabinetFileListGetDTO cabinetFileListGetDTO, BindingResult br) {
        XbbResponse<CabinetFileListGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileListGetVO cabinetFileListGetVO = cabinetFileService.getCabinetFileList(cabinetFileListGetDTO);
                response = new XbbResponse<>(cabinetFileListGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 文件柜 文件上传
     * @param cabinetFileSaveDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/6/25 19:43
     * @since v1.0
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid CabinetFileSaveDTO cabinetFileSaveDTO, BindingResult br) {
        XbbResponse<CabinetFileSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileSaveVO cabinetFileSaveVO = cabinetFileService.saveCabinetFiles(cabinetFileSaveDTO);
                response = new XbbResponse<>(cabinetFileSaveVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 删除文件柜 文件
     * @param cabinetFileDeleteDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/6/26 17:11
     * @since v1.0
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid CabinetFileDeleteDTO cabinetFileDeleteDTO, BindingResult br) {
        XbbResponse<CabinetFileDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileDeleteVO cabinetFileDeleteVO = cabinetFileService.deleteFiles(cabinetFileDeleteDTO);
                response = new XbbResponse<>(cabinetFileDeleteVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 下载文件柜 文件日志
     * @param cabinetFileDownLoadLogDTO
     * @param br
     * @return java.lang.String
     * @author youli.chen
     * @date 2019/6/26 17:11
     * @since v1.0
     */
    @RequestMapping(value = "/downLoad/log",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String downLoadLog(@RequestBody @Valid CabinetFileDownLoadLogDTO cabinetFileDownLoadLogDTO, BindingResult br) {
        XbbResponse<CabinetFileDownLoadLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CabinetFileDownLoadLogVO cabinetFileDownLoadLogVO = cabinetFileService.downLoadLog(cabinetFileDownLoadLogDTO);
                response = new XbbResponse<>(cabinetFileDownLoadLogVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
